﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;


public class ManagerConfig {

    public static string g(string name) {
        return System.Configuration.ConfigurationManager.AppSettings[name].ToString();
    }
    public static string bg(string name) {
        return System.Configuration.ConfigurationManager.AppSettings["background_"+name].ToString();
    }
    public static string sg(string name) {
        return System.Configuration.ConfigurationManager.AppSettings["site_"+name].ToString();
    }
    /// <summary>
    /// 后台用户登录
    /// </summary>
    /// <param name="loginName"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static System.Data.DataTable login(string loginName, string password) {
        System.Data.DataSet ds=new System.Data.DataSet();
        string sql ="select * from Users where loginName=@loginName and loginPassword=@password";
        return Codes.AccessHelper.ExecuteDataSet(Codes.connectionString.Manager, sql, new OleDbParameter[] { 
            new OleDbParameter("@loginName", loginName), 
            new OleDbParameter("@password", password) }).Tables[0];
    }
    /// <summary>
    /// trun:存在 false：不存在
    /// </summary>
    /// <param name="loginName"></param>
    /// <returns></returns>
    public static bool existsLoginName(string loginName) {
        int ret=0;
        string sql ="select count(1) from Users where loginName=@loginName";
        ret=(int)Codes.AccessHelper.ExecuteScalar(Codes.connectionString.Manager, sql, new OleDbParameter[] { new OleDbParameter("@loginName", loginName) });
        return ret>0;
    }
    /// <summary>
    /// 创建管理员
    /// </summary>
    /// <param name="nickname"></param>
    /// <param name="loginname"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static bool createManagerUser(string nickname, string loginname, string password) {
        string sql ="insert into Users(NickName,loginName,loginPassword,type,addtime) values(@nickname,@loginName,@password,1,'"+DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")+"')";
        return Codes.AccessHelper.ExecuteNonQuery(Codes.connectionString.Manager, sql, new OleDbParameter[]{
            new OleDbParameter("@nickname", nickname),
            new OleDbParameter("@loginName", loginname),
            new OleDbParameter("@password", password)
        })>0;
    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="id"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static bool updateUserPassword(string id, string password) {
        string sql ="update [Users] set [loginpassword]='"+password+"' where [ID]="+id;
        return Codes.AccessHelper.ExecuteNonQuery(Codes.connectionString.Manager, sql)>0;
    }
    public static bool updateUserInfo(string id, string nickname) {
        string sql ="update [Users] set [nickname]='"+nickname+"' where [ID]="+id;
        return Codes.AccessHelper.ExecuteNonQuery(Codes.connectionString.Manager, sql)>0;
    }

    public static bool massDeleteUser(string idList) {
        string sql ="delete from Users where ID in ("+idList+")";
        return Codes.AccessHelper.ExecuteNonQuery(Codes.connectionString.Manager, sql)>0;
    }

    public static System.Data.DataTable getUsers() {
        string sql ="select * from Users";
        return Codes.AccessHelper.ExecuteDataSet(Codes.connectionString.Manager, sql).Tables[0];
    }

    public static System.Data.DataTable getUserInfo(string id) {
        string sql ="select * from Users where ID=@id";
        return Codes.AccessHelper.ExecuteDataSet(Codes.connectionString.Manager, sql, new OleDbParameter[] { new OleDbParameter("@id", id) }).Tables[0];
    }
}
